package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.QueryPageDTO;
import com.itheima.reggie.common.Result;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @author Yan
 * @date 2022/2/21
 */

@Slf4j
@RestController
@RequestMapping("employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录
     * @param employee 员工
     * @return JSON统一响应结果
     */
    @PostMapping("login")
    public Result<Employee> login(HttpServletRequest request,@RequestBody Employee employee) {
        Result<Employee> login = employeeService.login(employee);

        Employee data = login.getData();
        //非空判断
        if (ObjectUtils.isEmpty(data)){
            return login;
        }
        request.getSession().setAttribute("employee",data.getId());

        return login;
    }

    /**
     * 员工退出
     *
     * @param request 请求对象
     * @return 是否成功
     */
    @PostMapping("logout")
    public Result<String> logout(HttpServletRequest request) {


        try {
            request.getSession().removeAttribute("employee");
            log.info("用户退出成功");
            return Result.success("退出成功");
        } catch (Exception e) {
            return Result.error("退出失败");
        }
    }

    /**
     * 添加员工
     * @param employee
     * @return
     */
    @PostMapping
    public Result<String> save(@RequestBody Employee employee) {
        //获取当前登录用户id
        //Long userId = (Long) request.getSession().getAttribute("employee");
        //employee.setCreateUser(userId);
        //employee.setUpdateUser(userId);

        return employeeService.saveEmployee(employee);
    }


    /**
     * 分页查询
     * @param queryPageDTO
     * @return
     */
    @GetMapping("page")
    public Result<Page<Employee>> page(QueryPageDTO queryPageDTO){
        return employeeService.page(queryPageDTO);
    }

    /**
     * 更新修改员工
     * @param employee
     * @return
     */
    @Transactional
    @PutMapping
    public Result<String> update(@RequestBody Employee employee){
        //Long userId = (Long) request.getSession().getAttribute("employee");
        //employee.setUpdateUser(userId);


        String name = employee.getName();
        String phone = employee.getPhone();
        String sex = employee.getSex();
        String idNumber = employee.getIdNumber();
        String username = employee.getUsername();

        Result<String> result;
        if (ObjectUtils.isEmpty(name)
                &&ObjectUtils.isEmpty(phone)
                &&ObjectUtils.isEmpty(sex)
                &&ObjectUtils.isEmpty(idNumber)
                &&ObjectUtils.isEmpty(username)){
            //调用启用禁用的方法
            result = employeeService.updateEmployeeStatusById(employee);
        }else {
            //调用修改的方法
            result =employeeService.update(employee);
        }


        return result;

    }

    /**
     * 根据id查询员工
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Employee> getEmployeeById(@PathVariable Long id){
        return employeeService.getEmployeeById(id);
    }


}
